﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using GreatB2C.Utility;
using GreatB2C.Business;
using GreatB2C.Config.Shop;

namespace GreatB2C.Web.Manage.SystemSet.Shop
{
    public partial class Choiceset : Inherit.ManagePage
    {
        /// <summary>
        ///  字典数据
        /// </summary>
        DataTable dtData = new DataTable( );

        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                dtData.Columns.Add( "ID" );
                dtData.Columns.Add( "Name" );
                dtData.Columns.Add( "DefaultValue" );
                dtData.Columns.Add( "IsOpen" );

                dtData.Clear( );

                if ( Request.QueryString[ "FieldName" ] != null && Request.QueryString[ "FieldName" ].ToString( ) != "" )
                {
                    LoadDiction( Request.QueryString[ "FieldName" ].ToString( ) );
                }
                else
                {
                    Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "参数传入错误或非法进入页面！" ) + "&rtn=Profile/MyWorktable.aspx" );
                    Response.End( );
                }
            }
        }

        protected void LoadDiction( string fieldName )
        {
            if ( fieldName.Trim( ) != "" )
            {
                Business.Dictionary dictBLL = new Dictionary( );
                Model.Dictionary dictModel = dictBLL.ListByFieldName( fieldName );

                if ( dictModel != null )
                {
                    if ( dictModel.FieldValue.Trim( ) != "" )
                    {
                        // 格式 ： 选项名|是否启用值|默认值
                        int i=0;
                        string[] parts = dictModel.FieldValue.ToString( ).Split( '$' );
                        foreach ( string part in parts )
                        {
                            if ( part.IndexOf( "|" ) > 0 )
                            {
                                string[] choises  = part.Split( '|' );
                                if ( choises.Length == 3 )
                                {
                                    DataRow dr = dtData.NewRow( );
                                    dr[ "ID" ] = i.ToString( );
                                    dr[ "Name" ] = choises[ 0 ];
                                    dr[ "DefaultValue" ] = choises[ 1 ];
                                    dr[ "IsOpen" ] = choises[ 2 ];

                                    dtData.Rows.Add( dr );
                                    i++;
                                }
                            }
                        }
                    }

                    while ( dtData.Rows.Count < 10 )
                    {
                        DataRow dr = dtData.NewRow( );
                        dr[ "ID" ] = dtData.Rows.Count.ToString( );
                        dr[ "Name" ] = "";
                        dr[ "DefaultValue" ] = "0";
                        dr[ "IsOpen" ] = "0";

                        dtData.Rows.Add( dr );
                    }

                    repDictionary.DataSource = dtData;
                    repDictionary.DataBind( );
                }
            }
        }

        protected void repDictionary_ItemDataBound( object sender , RepeaterItemEventArgs e )
        {
            if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
            {
                if ( dtData.Rows.Count > 0 )
                {
                    Label lblID = ( Label )e.Item.FindControl( "lblID" );
                    lblID.Text = dtData.Rows[ e.Item.ItemIndex ][ "ID" ].ToString( );

                    Controls.GroupRadioButton radIsOpen = ( Controls.GroupRadioButton )e.Item.FindControl( "radIsOpen" );
                    if ( dtData.Rows[ e.Item.ItemIndex ][ "IsOpen" ].ToString( ) == "1" )
                        radIsOpen.Checked = true;
                    else
                        radIsOpen.Checked = false;

                    CheckBox chbDefaultValue = ( CheckBox )e.Item.FindControl( "chbDefaultValue" );
                    if ( dtData.Rows[ e.Item.ItemIndex ][ "DefaultValue" ].ToString( ) == "1" )
                        chbDefaultValue.Checked = true;
                    else
                        chbDefaultValue.Checked = false;

                    TextBox txtTitle = ( TextBox )e.Item.FindControl( "txtTitle" );
                    txtTitle.Text = dtData.Rows[ e.Item.ItemIndex ][ "Name" ].ToString( );
                }
            }
        }


        protected void BtnSubmit_Click( object sender , EventArgs e )
        {
            string FieldValue = "";

            foreach ( RepeaterItem rpi in repDictionary.Items )
            {
                if ( rpi.ItemType == ListItemType.Item || rpi.ItemType == ListItemType.AlternatingItem )
                {
                    // 格式 ： 选项名|是否启用值|默认值
                    Label lblID = ( Label )rpi.FindControl( "lblID" );
                    Controls.GroupRadioButton radIsOpen = ( Controls.GroupRadioButton )rpi.FindControl( "radIsOpen" );
                    CheckBox chbDefaultValue = ( CheckBox )rpi.FindControl( "chbDefaultValue" );
                    TextBox txtTitle = ( TextBox )rpi.FindControl( "txtTitle" );

                    if ( txtTitle.Text.Trim( ) != "" )
                    {
                        if ( FieldValue.Trim( ) != "" )
                            FieldValue += "$";

                        string thisvalue = txtTitle.Text.Trim( )
                            + "|" + ( chbDefaultValue.Checked ? "1" : "0" )
                            + "|" + ( radIsOpen.Checked ? "1" : "0" );

                        FieldValue += thisvalue;
                    }
                }
            }

            if ( FieldValue.Trim( ) != "" )
            {
                Model.Dictionary dictModel = new GreatB2C.Model.Dictionary( );
                dictModel.FieldName = Request.QueryString[ "FieldName" ].ToString( );
                dictModel.FieldValue = FieldValue;

                Business.Dictionary dictBLL = new Dictionary( );
                bool isSuccess = dictBLL.UpdateFieldValue( dictModel );
                if ( isSuccess )
                {
                    Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "数据字典维护成功！" ) + "&rtn=SystemSet/Shop/Choiceset.aspx?FieldName=" + Request.QueryString[ "FieldName" ].ToString( ) );
                }
                else
                {
                    Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "数据字典维护失败！" ) + "&rtn=SystemSet/Shop/Choiceset.aspx?FieldName=" + Request.QueryString[ "FieldName" ].ToString( ) );
                }
            }
        }
    }
}
